package top.weiliang.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.weiliang.springboot3.base.PageBean;
import top.weiliang.springboot3.base.R;
import top.weiliang.springboot3.dao.TbClassMapper;
import top.weiliang.springboot3.entity.TbClass;
import top.weiliang.springboot3.service.TbClassService;
import top.weiliang.springboot3.vo.PageVO;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbClassServiceImpl implements TbClassService {
    private final TbClassMapper tbClassMapper;

    private TbClass queryByCname(String cname) {
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
        wrapper.eq("cname",cname);
        //查询单一记录的方法
        TbClass tbClass = tbClassMapper.selectOne(wrapper);
        return tbClass;
    }

    @Override
    public R<String> insert(TbClass tbClass){
//        先效验名称是否存在
        TbClass check = queryByCname(tbClass.getCname());
        if(check != null){
            return R.fail("班级名称已存在",null);
        }

        int i=tbClassMapper.insert(tbClass);
        return i==1 ?R.ok("添加成功",null) :R.fail("添加失败",null);
    }
    @Override
    public R<List<TbClass>>query(TbClass tbClass){
        //动态sql(查询条件）
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
        //如果cname有传入，就添加过滤
        //StringUtils.hasText表示字符串不能为null,或者全部是空白字符
        wrapper.like(StringUtils.hasText(tbClass.getCname()),"cname", tbClass.getCname());
        wrapper.like(StringUtils.hasText(tbClass.getDescription()),"description", tbClass.getDescription());
        List<TbClass>list=tbClassMapper.selectList(wrapper);
        return R.ok(list);
    }

    @Override
    public R<PageVO<TbClass>>queryPage(PageBean pageBean, TbClass tbClass){
        //分页的参数
        IPage<TbClass> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        //动态sql(查询条件）
        QueryWrapper<TbClass> wrapper = new QueryWrapper<>();
        //添加排序(desc是逆序,asc是正序)
        wrapper.orderByDesc("create_at");
        //如果cname有传入，就添加过滤
        //StringUtils.hasText表示字符串不能为null,或者全部是空白字符
        wrapper.like(StringUtils.hasText(tbClass.getCname()),"cname", tbClass.getCname());
        wrapper.like(StringUtils.hasText(tbClass.getDescription()),"description", tbClass.getDescription());
        //分页查询的结果是Page对象
        page = tbClassMapper.selectPage(page, wrapper);
//        //分页的返回值
//        PageVO<TbClass> vo = new PageVO<>();
//        vo.setList(page.getRecords());
//        pageBean.setTotal(page.getTotal());
//        pageBean.setPageCount(page.getPages());
//        vo.setPageBean(pageBean);
        return R.ok(page);
    }

    @Override
    public R<TbClass>queryById(Integer id){
        TbClass tbClass=tbClassMapper.selectById(id);
        return tbClass==null ?R.fail("查询失败",null) :R.ok(tbClass);
    }

    @Override
    public R<String>update(TbClass tbClass){
        //效验名称是否存在
        TbClass check = queryByCname(tbClass.getCname());
        if (check != null && !check.getId().equals(tbClass.getId())){
            return R.fail("班级名称已存在",null);
        }

        int i=tbClassMapper.updateById(tbClass);
        return i==1 ?R.ok("修改成功",null) :R.fail("修改失败",null);
    }

    @Override
    public R<String>delete(Integer id){
        int i=tbClassMapper.deleteById(id);
        return i==1 ?R.ok("删除成功",null) :R.fail("删除失败",null);
    }
}
